package com.servlet.article;

import com.entity.Article;
import com.entity.Code;
import com.service.ArticleService;
import com.utils.JsonUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 分页查询数据，根据提供的pageNum页数，offset每页显示的数据返回json格式数据
 * pageNum 页码
 * offset 每页显示的条数
 */
@WebServlet(name = "ArticlePage", value = "/article")
public class ArticlePage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        ArticleService articleService = new ArticleService();
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        int offset = Integer.parseInt(request.getParameter("offset"));
        List<Article> acticlePage = articleService.findActiclePage(pageNum, offset);
        Code code = new Code();
        code.setMsg("分页查询成功");
        code.setCode("200");
        code.setData(acticlePage);
        String s = jsonUtil.JavaToJson(code);
        response.getWriter().print(s);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }
}
